package org.oliver.crp.samtrack.domain;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
//import javax.persistence.Entity;
//import javax.persistence.PrePersist;

import org.oliver.crp.samtrack.server.PMF;

/**
 * An application user, named with a prefix to avoid confusion with GAE User type
 */
@PersistenceCapable
public class AppUser 
{

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	@Persistent
	private Integer version = 0;
	@Persistent
	private String name;

	public AppUser() {}
	
	public AppUser(String name) {
		this.name = name;
	}
	
	public Long getId() {
		return this.id;
	}
	
	public void persist() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			
			pm.makePersistent(this);
			
			tx.commit();
		}
		catch (Exception e) {
			
		}
		finally {
			
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();			
		}
	}
	

	/**
	 * 
	 * @return All existing AppUsers 
	 */
	public static List<AppUser> findAllAppUser() {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try {
			// TODO implement method
			return null;
		}
		finally {
			pm.close();
		}
	}
	
	public static AppUser findAppUser(Long id) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try {
			AppUser a = pm.getObjectById(AppUser.class, id);
			return a;
			
		} finally {
			pm.close();
		}

	}

	public Integer getVersion() {
		return version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}

